// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   LMSCMIServlet.java

package com.iphaeton.lms.course.scorm12.runtime.core;

import com.iphaeton.lms.course.scorm12.datamodel.Element;
import com.iphaeton.lms.course.scorm12.datamodel.SCODataManager;
import com.iphaeton.lms.course.scorm12.datamodel.cmi.CMICore;
import com.iphaeton.persistence.spring.CourseDBService;
import java.io.*;
import java.sql.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

public class LMSCMIServlet extends HttpServlet
{

    public LMSCMIServlet()
    {
        LMSSCODataFile = "\\LMSSampleDB\\User01\\Course01\\Lesson01\\scodata";
        LMSUser01 = "\\LMSSampleDB\\User01\\Course01\\Lesson01";
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        try
        {
            logger.debug("requested session: " + request.getRequestedSessionId());
            if(request.isRequestedSessionIdFromCookie())
                logger.debug("requested session id from cookie");
            if(logger.isDebugEnabled())
            {
                logger.debug("query string: " + request.getQueryString());
                logger.debug("header string: " + request.getContextPath());
                logger.debug("cookie header: " + request.getHeader("Cookie"));
            }
            if(logger.isDebugEnabled())
            {
                for(Enumeration e = request.getHeaderNames(); e.hasMoreElements(); logger.debug(e.nextElement()));
            }
            logger.debug("POST received by LMSCMIServlet");
            HttpSession session = request.getSession(false);
            if(session == null)
                logger.debug("this is bad - no session in cmi servlet");
            else
                logger.debug("session id is: " + session.getId());
            logger.debug("about to check attributes");
            if(logger.isDebugEnabled())
            {
                for(Enumeration e = request.getHeaderNames(); e.hasMoreElements(); logger.debug(e.nextElement()));
            }
            scoID = (String)session.getAttribute("SCOID");
            courseID = (String)session.getAttribute("COURSEID");
            userID = (String)session.getAttribute("USERID");
            logger.debug("session scoID: " + scoID);
            scoFile = "\\SampleRTEFiles\\" + userID + "\\" + courseID + "\\" + scoID;
            ObjectInputStream in = new ObjectInputStream(request.getInputStream());
            logger.debug("Created REQUEST object INPUT stream successfully");
            ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
            logger.debug("Created RESPONSE object OUTPUT stream successfully");
            String command = (String)in.readObject();
            logger.debug("Command to LMSCMIServlet is: " + command);
            if(command.equalsIgnoreCase("cmiputcat"))
            {
                logoutFlag = false;
                SCODataManager inSCOData = (SCODataManager)in.readObject();
                logger.debug("LMSCMIServlet read in the SCODataManager object");
                HandleData(inSCOData);
                if(logoutFlag)
                    session.setAttribute("EXITFLAG", "true");
                else
                    session.removeAttribute("EXITFLAG");
            } else
            if(command.equalsIgnoreCase("cmigetcat"))
            {
                FileInputStream fi = new FileInputStream(scoFile);
                logger.debug("Created LMSSCODataFile FILE input stream successfully");
                ObjectInputStream file_in = new ObjectInputStream(fi);
                logger.debug("Created OBJECT input stream successfully");
                scoData = (SCODataManager)file_in.readObject();
                scoData.getCore().setSessionTime("00:00:00.0");
                file_in.close();
                out.writeObject(scoData);
                System.out.print("LMSCMIServlet processed get for SCO Data\n");
            } else
            {
                String err_msg = "invalid command";
                out.writeObject(err_msg);
            }
            in.close();
            out.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private void HandleData(SCODataManager inSCOData)
    {
        Connection conn = null;
        PreparedStatement stmtUpdateUserSCO = null;
        PreparedStatement stmtSelectUserSCO = null;
        try
        {
            String lessonStatus = new String();
            String lessonExit = new String();
            String lessonEntry = new String();
            CMICore lmsCore = inSCOData.getCore();
            if(lmsCore.getExit().getValue().equalsIgnoreCase("logout"))
                logoutFlag = true;
            lessonStatus = lmsCore.getLessonStatus().getValue();
            lessonExit = lmsCore.getExit().getValue();
            lessonEntry = lmsCore.getEntry().getValue();
            inSCOData.setCore(lmsCore);
            if(logger.isDebugEnabled())
            {
                logger.debug("Saving Data to the File ...");
                logger.debug("Prior to Save");
                logger.debug("The SCO Data Manager for the current SCO contains the following:");
                inSCOData.getCore().showData();
            }
            FileOutputStream fo = new FileOutputStream(scoFile);
            ObjectOutputStream out_file = new ObjectOutputStream(fo);
            out_file.writeObject(inSCOData);
            out_file.close();
            String sqlUpdateUserSCO = "UPDATE UserSCOInfo SET LessonStatus = ?, ExitStatus = ?, Entry = ? WHERE UserID = ? AND CourseID = ? AND SCOID = ?";
            String sqlSelectUserSCO = "SELECT * FROM UserSCOInfo WHERE UserID = ? AND CourseID = ? AND SCOID = ?";
            conn = CourseDBService.getConnection();
            stmtUpdateUserSCO = conn.prepareStatement(sqlUpdateUserSCO);
            stmtSelectUserSCO = conn.prepareStatement(sqlSelectUserSCO);
            if(logger.isDebugEnabled())
            {
                logger.debug("about to update userSCOInfo");
                logger.debug("userID: " + userID);
                logger.debug("courseID: " + courseID);
            }
            synchronized(stmtUpdateUserSCO)
            {
                stmtUpdateUserSCO.setString(1, lessonStatus);
                stmtUpdateUserSCO.setString(2, lessonExit);
                stmtUpdateUserSCO.setString(3, lessonEntry);
                stmtUpdateUserSCO.setString(4, userID);
                stmtUpdateUserSCO.setString(5, courseID);
                stmtUpdateUserSCO.setString(6, scoID);
                stmtUpdateUserSCO.executeUpdate();
            }
            ResultSet userSCORS = null;
            synchronized(stmtSelectUserSCO)
            {
                stmtSelectUserSCO.setString(1, userID);
                stmtSelectUserSCO.setString(2, courseID);
                stmtSelectUserSCO.setString(3, scoID);
                userSCORS = stmtSelectUserSCO.executeQuery();
            }
            userSCORS.next();
            String newStatus = userSCORS.getString("LessonStatus");
            logger.debug("the status in sequencingEngine after set is: " + newStatus);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                stmtUpdateUserSCO.close();
                stmtSelectUserSCO.close();
                if(conn != null)
                    conn.close();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return;
    }

    private static transient Logger logger;
    private String LMSSCODataFile;
    private String LMSUser01;
    private String scoFile;
    private String userID;
    private String courseID;
    private String scoID;
    private boolean logoutFlag;
    private SCODataManager scoData;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.course.scorm12.runtime.core.LMSCMIServlet.class.getName());
    }
}
